/*****************************************************************************************
   
   Copyright (c) Microsoft Corporation. All rights reserved.

   Use of this code sample is subject to the terms of the Microsoft
   Permissive License, a copy of which should always be distributed with
   this file.  You can also access a copy of this license agreement at:
   http://www.microsoft.com/resources/sharedsource/licensingbasics/permissivelicense.mspx

 ****************************************************************************************/

using System;

namespace GrooveWebServicesV12Helpers.Forms
{
	/// <summary>
	/// Represents a Form in a FormsTool.
	/// </summary>
	public class Form : GrooveWebServicesV12HelpersBase
	{
		private GrooveWebServicesV12Helpers.Forms.FormsTool m_FormsTool;
		
		private GrooveFormsWebService.Form m_Form;

		public Form(GrooveWebServicesV12Helpers.Forms.FormsTool i_FormsTool, GrooveWebServicesV12Helpers.GrooveFormsWebService.Form i_Form)
			: base(i_Form.Name, i_Form.URI, i_FormsTool.IdentityURL, i_FormsTool.Context)
		{
			m_FormsTool = i_FormsTool;
			m_Form = i_Form;
		}

		/// <summary>
		/// The FormsTool which contains this Form.
		/// </summary>
		public GrooveWebServicesV12Helpers.Forms.FormsTool FormsTool
		{
			get
			{
				return m_FormsTool;
			}
		}

		/// <summary>
		/// The ID of this Form.
		/// </summary>
		public string ID
		{
			get
			{
				return m_Form.ID;
			}
		}

		/// <summary>
		/// Array of Fields used in this Form.
		/// </summary>
		public GrooveWebServicesV12Helpers.Forms.Field[] Fields
		{
			get
			{
				System.Collections.ArrayList FieldsArrayList = new System.Collections.ArrayList();

                //  Get initialized reference to web service
				GrooveFormsWebService.GrooveForms FormsWebService = GrooveWebServicesV12Helpers.Forms.FormsTool.CreateInitializedFormsWebService
					(
					m_IdentityURL,
					this.URI,
					Context
					);

				System.DateTime BeginDateTime = System.DateTime.Now;

				//  Ask service to provide fields
				GrooveFormsWebService.FormsField2[] FormsFields = FormsWebService.ReadFormFields2();

				if (GrooveWebServicesV12Helpers.Context.TraceEnabled)
				{
					System.TimeSpan TimeSpan = System.DateTime.Now.Subtract(BeginDateTime);
					System.Diagnostics.Debug.WriteLine("FormsWebService::ReadFormFields2, " + TimeSpan.TotalMilliseconds + " ms");
				}

				//  Create array list of fields
				foreach (GrooveFormsWebService.FormsField2 FormsField in FormsFields)
				{
					GrooveWebServicesV12Helpers.Forms.Field NewField = new GrooveWebServicesV12Helpers.Forms.Field
						(
						this,
						FormsField
						);

					FieldsArrayList.Add(NewField);
				}

				//  Return array from array list
				return (GrooveWebServicesV12Helpers.Forms.Field[])FieldsArrayList.ToArray(typeof(GrooveWebServicesV12Helpers.Forms.Field));
			}
		}

		#region GrooveWebServicesV12HelpersBase Implementation

		public override string BindableURL
			{
			get
			{
				if (System.String.IsNullOrEmpty(m_BindableURL))
					m_BindableURL = m_FormsTool.BindableURL + "/Form#" + this.ID;

				return m_BindableURL;
			}
		}

		public override object BindToObject(ref string[] i_URLParts, int i_URLPartIndex)
		{
			object BindResult = null;
			string RelativeURL = i_URLParts[i_URLPartIndex];
			string[] RelativeURLParts = RelativeURL.Split(new char[]{'#'});

			string ObjectType = RelativeURLParts[0];

			if (ObjectType == "Field")
			{
				string FieldURI = m_FormsTool.URI + "/DataModelDelegate/FieldID=" + RelativeURLParts[1];
				FieldURI = FieldURI.Replace("Tools/", "Forms/");

				try
				{
					//  ---------------------------------------------------------------------------------------
					//  HACK ALERT:  Since there is no ReadField method, must read all fields and do uri match

					foreach (GrooveWebServicesV12Helpers.Forms.Field Field in this.Fields)
					{
						if (Field.URI == FieldURI)
						{
							BindResult = Field;
							break;
						}
					}

					//  ---------------------------------------------------------------------------------------
				}
				catch (System.Exception Exception)
				{
					throw new System.Exception("Unable to find Field '" + FieldURI +"' (" + Exception.Message + ")");
				}
			}
			else
				throw new System.Exception("Unknown form url part - '" + ObjectType + "'");

			return BindResult;
		}

		#endregion
	}

}
